package com.example.guli.service.trade.controller.api;


import com.example.guli.common.base.result.R;
import com.example.guli.common.base.util.jwt.JwtInfo;
import com.example.guli.common.base.util.jwt.JwtUtils;
import com.example.guli.service.trade.entity.Order;
import com.example.guli.service.trade.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author shunxuan
 * @since 2020-09-10
 */
@RestController
@CrossOrigin
@Api(description = "网站订单管理")
@RequestMapping("/api/trade/order")
@Slf4j
public class ApiOrderController {
    @Autowired
    private OrderService orderService;

    @ApiOperation("新增订单-调用其他微服务的接口edu/ucenter")
    @PostMapping("auth/save/{courseId}")
    public R save(
            @PathVariable String courseId,
            HttpServletRequest httpServletRequest
    ){
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(httpServletRequest);
        String orderId= orderService.saveOrder(courseId,jwtInfo.getId());
        return R.ok().data("data",orderId);
    }

    @ApiOperation("获取新增订单详情")
    @GetMapping("auth/get/{orderId}")
    public R get(
            @PathVariable String orderId,
            HttpServletRequest httpServletRequest
    ){
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(httpServletRequest);
        Order order =orderService.getByOrderId(orderId,jwtInfo.getId());
        return R.ok().data("data",order);
    }

    @ApiOperation( "判断课程是否购买")
    @GetMapping("auth/is-buy/{courseId}")
    public R isBuyByCourseId(@PathVariable String courseId, HttpServletRequest httpServletRequest) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(httpServletRequest);
        Boolean isBuy = orderService.isBuyByCourseId(courseId, jwtInfo.getId());
        return R.ok().data("data", isBuy);
    }

    @ApiOperation(value = "获取当前用户订单列表")
    @GetMapping("auth/list")
    public R list(HttpServletRequest httpServletRequest) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(httpServletRequest);
        List<Order> list = orderService.selectByMemberId(jwtInfo.getId());
        return R.ok().data("data", list);
    }
    @ApiOperation(value = "删除订单")
    @DeleteMapping("auth/remove/{orderId}")
    public R remove(@PathVariable String orderId, HttpServletRequest httpServletRequest) {
        JwtInfo jwtInfo = JwtUtils.getMemberIdByJwtToken(httpServletRequest);
//        boolean result=false;
//        if(jwtInfo!=null){
//             result = orderService.removeById(orderId);
//        }
        boolean result = orderService.removeById(orderId, jwtInfo.getId());
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation(value = "根据订单号查询订单")
    @DeleteMapping("auth/get/{orderNo}")
    public R getById(
            @PathVariable String orderNo){
        Order order = orderService.getOrderByOrderNo(orderNo);
        return R.ok().data("data",order);

    }




}

